package com.base.cn.platform.os.service.statistics.pvuv;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.statistics.pvuv.ZStatisticsUniqueVisitorCountDao;
import com.base.cn.platform.os.entity.statistics.pvuv.ZStatisticsUniqueVisitorCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * UV统计Biz
 *
 * @author s.li
 * @date 2019/04/17
 */
@Service
public class ZStatisticsUniqueVisitorCountBiz extends BaseBiz<ZStatisticsUniqueVisitorCount, ZStatisticsUniqueVisitorCountDao> {
    @Autowired
    private ZStatisticsUniqueVisitorCountDao zStatisticsUniqueVisitorCountDao;

    /**
     * 添加UV统计数据
     * @param zStatisticsUniqueVisitorCountList
     */
    @Transactional
    public synchronized void createUniqueVisitorCount(List<ZStatisticsUniqueVisitorCount> zStatisticsUniqueVisitorCountList){
        if(ObjectUtils.isNotEmpty(zStatisticsUniqueVisitorCountList)){
            String timeWhereSql = zStatisticsUniqueVisitorCountList.stream().map(e-> "DATE_FORMAT(requestTime,'%Y-%m-%d')='"+ DateUtils.format(e.getRequestTime(),"yyyy-MM-dd")+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
            List<ZStatisticsUniqueVisitorCount> uniqueVisitorCountList = this.find(timeWhereSql,null,null);
            List<ZStatisticsUniqueVisitorCount> updateList = new ArrayList<>();
            List<ZStatisticsUniqueVisitorCount> addList = new ArrayList<>();
            if(ObjectUtils.isNotEmpty(uniqueVisitorCountList)){
                Map<String,ZStatisticsUniqueVisitorCount> oldUniqueVisitorMap = uniqueVisitorCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getRequestTime(),"yyyy-MM-dd"), e->e));
                Map<String,ZStatisticsUniqueVisitorCount> newUniqueVisitorMap = zStatisticsUniqueVisitorCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getRequestTime(),"yyyy-MM-dd"), e->e));
                newUniqueVisitorMap.forEach((k,v)->{
                    ZStatisticsUniqueVisitorCount oldData = oldUniqueVisitorMap.get(k);
                    if(ObjectUtils.isNotEmpty(oldData)){
                        oldData.setRequestCount(v.getRequestCount());
                        updateList.add(oldData);
                    }else{
                        addList.add(v);
                    }
                });
            }else{
                addList.addAll(zStatisticsUniqueVisitorCountList);
            }
            if(ObjectUtils.isNotEmpty(addList)){
                this.batchSave(addList);
            }
            if(ObjectUtils.isNotEmpty(updateList)){
                this.updateBatch(updateList);
            }
        }
    }

    /**
     * 获取某天的UV量
     * @param nowDate 时间
     * @return ZStatisticsUniqueVisitorCount
     */
    public ZStatisticsUniqueVisitorCount getOneDayUniqueVisitorCount(Date nowDate){
        String nowTime = DateUtils.format(nowDate,"yyyy-MM-dd");
        //录前的访问量（UV）
        ZStatisticsUniqueVisitorCount uniqueVisitorCount = new ZStatisticsUniqueVisitorCount();
        uniqueVisitorCount.setRequestTime(nowDate);
        ZStatisticsUniqueVisitorCount _uniqueVisitorCount = this.findOne(" requestTime = '"+nowTime+"'",null);
        if(_uniqueVisitorCount!=null){
            uniqueVisitorCount.setRequestTime(_uniqueVisitorCount.getRequestTime());
            uniqueVisitorCount.setRequestCount(_uniqueVisitorCount.getRequestCount());
        }
        return uniqueVisitorCount;
    }

    /**
     * 查询平台UV数据（以天方式）
     * @param timeSql
     * @return List<ZStatisticsUniqueVisitorCount>
     */
    public List<ZStatisticsUniqueVisitorCount> queryUniqueVisitorCountList(String timeSql){
        return zStatisticsUniqueVisitorCountDao.queryUniqueVisitorCountList(ZStatisticsUniqueVisitorCount.class,timeSql);
    }
}
